﻿using System;
using System.Collections;
using System.Drawing.Design;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using com.code.Editors.Code;

namespace com.Editors
{
    public partial class DataForm : Form 
    {
       static string conString = "";
       ArrayList list = null;
       private bool isDone = false;
       IServiceProvider provider = null;
       IDictionaryService dictionaryService = null;
       ArrayList listTables = null;

       public DataForm(IServiceProvider provider)
        {
            InitializeComponent();
            this.GetDataString();
            this.provider = provider;
            dictionaryService = provider.GetService(typeof(IDictionaryService)) as IDictionaryService;
            
            listTables = new ArrayList();
        }

        /**public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }*/

        private string GetDataString()
        {
            Microsoft.Data.ConnectionUI.DataConnectionDialog dialog = new
Microsoft.Data.ConnectionUI.DataConnectionDialog();

            Microsoft.Data.ConnectionUI.DataSource.AddStandardDataSources(dialog);

            Microsoft.Data.ConnectionUI.DataConnectionDialog.Show(dialog);
            conString = dialog.ConnectionString;
            if (dialog.DialogResult == DialogResult.OK)
            {
                BindData();
            }
            return dialog.ConnectionString;
        }

        //public override object EditValue(ITypeDescriptorContext context,
        //    IServiceProvider provider, object value)
        //{
        //    return "conStr";
        //} 

        private void BindData()
        {
            SchemaDAL obj = new SchemaDAL(conString);
            list = obj.GetTables();
            
            foreach (GuidDataTable table in list)
            {
                this.lstDBTables.Items.Add(table.TableName);

            }
            //this.lstDBTables.DataSource = list;
            
        }

        
        public static string GetConStr()
        {
            return conString;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            TableDataForm form = new TableDataForm(provider);
            form.Show();

            ArrayList tbls = new ArrayList();
            foreach (string o in lstToTables.Items)
            {
               foreach(GuidDataTable tbl in list)
                   if (tbl.TableName == o)
                   {
                       if (tbl.TableName == cmbParentTable.Text)
                           tbl.IsMaster = true;
                       listTables.Add(tbl);
                   }
                
            }
            form.setDataTables(listTables);
            this.isDone = true;
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            if (lstDBTables.SelectedIndex > -1)
            {
                lstToTables.Items.Add(lstDBTables.SelectedItem);
                cmbParentTable.Items.Add(lstDBTables.SelectedItem);
                //lstDBTables.Items.Remove(lstDBTables.SelectedItem);                
            }                        
        }

        private void btnMoveAll_Click(object sender, EventArgs e)
        {
            lstToTables.Items.Clear();
            foreach (GuidDataTable o in list)
            {
                lstToTables.Items.Add(o.TableName);
                cmbParentTable.Items.Add(o.TableName);
            }
            //lstDBTables.Items.Clear();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            if (lstToTables.SelectedIndex > -1)
            {
                //lstDBTables.Items.Add(lstToTables.SelectedItem);
                cmbParentTable.Items.Remove(lstToTables.SelectedItem);
                lstToTables.Items.Remove(lstToTables.SelectedItem);
            }
            
        }

        private void btnBackAll_Click(object sender, EventArgs e)
        {
            //lstDBTables.Items.Clear();
            //cmbParentTable.Items.Clear();
            //foreach (GuidDataTable o in list)
            //    lstDBTables.Items.Add(o.TableName);
            lstToTables.Items.Clear();
        }

        public bool isFormDone()
        {
            return isDone;
        }

        public string GetData()
        {
            if (isDone)
            {
                return "OK";
            }
            else {
                return "Not OK";
            }
        }

        private void lstToTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach(GuidDataTable table in list)
            {
                if (table.TableName == lstToTables.Text)
                {
                    txtTableAlias.Text = table.TableAlias;
                    //chkIsParent.Checked = table.IsMaster;
                }
            }
            
        }

        private void chkIsParent_CheckedChanged(object sender, EventArgs e)
        {            
            if (lstToTables.SelectedIndex < 0)
            {
                MessageBox.Show("Please select a table");
                //chkIsParent.Checked = false;
            }
            else
            {
                foreach (GuidDataTable table in list)
                {
                    //if (table.TableName == lstToTables.Text)
                    //    table.IsMaster = chkIsParent.Checked;
                    //else
                    //    table.IsMaster = ! chkIsParent.Checked;

                }
            }
        }
    }
}
